package com.example.asus.yuejian_10.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import com.example.asus.yuejian_10.Config;
import com.example.asus.yuejian_10.MainActivity;
import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.RetrofitSeveice;
import com.example.asus.yuejian_10.adapter.ClubclassAdapter;
import com.example.asus.yuejian_10.adapter.OrderAdapter;
import com.example.asus.yuejian_10.bean.UserOrder;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class OrderActivity extends AppCompatActivity {
    private OrderAdapter adapter;
    private RecyclerView rv;
    private RetrofitSeveice service;
    private String Uid;
    private TextView order_null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        rv = (RecyclerView) findViewById(R.id.rv_order);
        order_null = (TextView) findViewById(R.id.order_null);
        MainActivity mainActivity = new MainActivity();
        Uid = mainActivity.getUserId();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.URL)
                .addConverterFactory(GsonConverterFactory.create()).build();
        service = retrofit.create(RetrofitSeveice.class);

        Call<List<UserOrder>> call = service.getallorder(Integer.parseInt(Uid));
        call.enqueue(new Callback<List<UserOrder>>() {
            @Override
            public void onResponse(Call<List<UserOrder>> call, Response<List<UserOrder>> response) {
                List<UserOrder> orderList = response.body();

                if (orderList.size()==0){
                    Log.v("lkkk","null");
                    order_null.setText("暂时没有订单");
                }
                else {
                    adapter = new OrderAdapter(OrderActivity.this,orderList);

                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(OrderActivity.this);

                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

                    rv.setLayoutManager(linearLayoutManager);
                    rv.setAdapter(adapter);
                    rv.setItemAnimator(new DefaultItemAnimator());
                }
            }

            @Override
            public void onFailure(Call<List<UserOrder>> call, Throwable t) {

            }
        });
    }
}
